package com.yd.mvc.pc.jewellery;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.jewellery.jewelleryPageService.WebOldService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Old;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.OldParam;
import com.yd.domain.sys.Logger;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by sunxianhui on 2018/5/15.
 */
@RestController
@RequestMapping(value = "/api/webOld")
public class WebOldController {
    @Autowired
    private WebOldService webOldService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc: 录入旧物消息
     * @Date:  2018/3/8
     * @Param
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addDic(@RequestBody Old old,HttpServletRequest request) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        String userName="";
        if(user!=null){
//            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.OLD_MANAGER);//验证权限
            userName=user.getUserName();
        }
        String suff="ID:"+old.getId()+";名称:"+old.getTitle()+";重量:"+old.getPerWeight();
        try {
            old.setAddUser(user.getUserId());//录入人
            webOldService.addOld(old);
            loggerService.addLogger(new Logger("新增旧物记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("新增旧物记录",userName,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
            e.printStackTrace();
        }
    }

    /**
     * @Author: zy
     * @Desc:查询数据旧物回收列表
     * @Date:  2018/3/8
     */
    @RequestMapping(value = "/getList/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getOldList(@RequestBody OldParam oldParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        oldParam.setAddUser(user.getUserId());
        PageModel pageModel = null;
        if(user!=null) {
            try {
                pageModel = webOldService.getOldList(oldParam, pageIndex, pageSize);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException(500, "获取旧物信息失败");
            }
        }
        return pageModel;
    }

    /**
     * 查询旧物
     * @param id
     */
    @RequestMapping(value = "/getOldById/{id}", method = RequestMethod.GET)
    public Old getGoodsById(@PathVariable("id")String id)  {
        Old old=webOldService.getOldById(id);
        return old;
    }

    /**
     * @Author: zy
     * @Desc: 数据修改
     * @Date:  2018/3/8
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public void updateOld(@RequestBody Old old,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+old.getId()+";名称:"+old.getTitle()+";重量:"+old.getPerWeight()+";商品类型:"+old.getType();
        try {
            webOldService.updateOld(old);
            loggerService.addLogger(new Logger("修改旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("修改旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
            e.printStackTrace();
        }
    }

    /**
     * 删除图片
     * @param id
     */
    @RequestMapping(value = "/updateIconUrl/{id}", method = RequestMethod.GET)
    public void updateIconUrl(@PathVariable("id")String id)  {
        webOldService.updateIconUrl(id);
    }

    /**
     * @Author: zy
     * @Desc: 删除数据
     * @Date:  2018/3/8
     * @Param
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteOld(@PathVariable("id") String id,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+id;
        try {
            webOldService.deleteOld(id);
            loggerService.addLogger(new Logger("删除旧物记录",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("删除旧物记录",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
            e.printStackTrace();
        }
    }
}
